package org.ix.boot.mvc;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
import org.ix.boot.app.HomeServlet;

public class SpringApplication {
    public static void run() throws LifecycleException {
        // tomcat SPI
        // classpath:/META-INF/services/javax.servlet.ServletContainerInitializer
        /*
        todo 将 HandlesTypes 中 WebApplicationInitializer 自己实现并加载
        AppInitializer
        @HandlesTypes({WebApplicationInitializer.class}) // Servlet启动时，加载实现了WebApplicationInitializer的类，并将这些类传递到 onStartup(webAppInitializerClasses) 中
        public class SpringServletContainerInitializer implements ServletContainerInitializer {
            // ...
            public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
            // ...
            }
            // ...
        }
        */
        Tomcat tomcat = new Tomcat();
        Connector connector = new Connector();
        connector.setPort(8088);
        connector.setURIEncoding("UTF-8");
        tomcat.getService().addConnector(connector);

        // contextPath  http://localhost:8088/project/xxx
        tomcat.addWebapp("/project", "/opt");
        tomcat.start();
        tomcat.getServer().await();
    }
}
